﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UtilZ.Dotnet.Ex.Log;

namespace DotnetWinFormApp
{
    public partial class TestSpan : Form
    {
        public TestSpan()
        {
            InitializeComponent();
        }

        private void TestSpan_Load(object sender, EventArgs e)
        {

        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            SpanItem[] spanItems = new SpanItem[6];
            for (int i = 0; i < spanItems.Length; i++)
            {
                spanItems[i] = new SpanItem(i + 1, $"yf_风瑕{i + 1}");
            }

            try
            {
                //Marshal.AddRef();
                IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement<SpanItem>(spanItems, 2);
                //SpanItem[] retArr = Marshal.GetObjectsForNativeVariants<SpanItem>(ptr, 2);
                object ret = Marshal.GetObjectForNativeVariant(ptr);
                //object  = System.Runtime.CompilerServices.Unsafe.
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }
    }

    public class SpanItem
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public SpanItem(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }

        public SpanItem()
        {

        }
    }
}
